package com.uniqueyi.demo;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.journeyapps.barcodescanner.ViewfinderView;

/**
 * Created by
 * author:  cwy.
 * date:    2017/12/5.  9:26.
 * prj:     demo.
 */


public class ScanAty extends AppCompatActivity implements DecoratedBarcodeView.TorchListener {
    private CaptureManager captureManager;
    private boolean isLightOn = false;
    DecoratedBarcodeView mDBV;
    ImageView iv_light;
    ViewfinderView zxing_viewfinder_view;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_sacn);
        mDBV = (DecoratedBarcodeView) findViewById(R.id.dbv_custom);
        iv_light = (ImageView) mDBV.findViewById(R.id.iv_light);
        zxing_viewfinder_view = (ViewfinderView) mDBV.findViewById(R.id.zxing_viewfinder_view);
        mDBV.setStatusText("");
        mDBV.setTorchListener(this);
//        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
//        zxing_viewfinder_view.drawResultBitmap(bmp);
        // 如果没有闪光灯功能，就去掉相关按钮
        if (!hasFlash()) {
            iv_light.setVisibility(View.GONE);
        }
        iv_light.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chagnePic(!isLightOn);
                if (isLightOn) {
                    mDBV.setTorchOff();
                } else {
                    mDBV.setTorchOn();
                }
            }
        });
        //重要代码，初始化捕获
        captureManager = new CaptureManager(this, mDBV);
        captureManager.initializeFromIntent(getIntent(), savedInstanceState);
        captureManager.decode();
    }

    private void chagnePic(boolean isOn) {
        if (isOn) {
            iv_light.setImageResource(R.mipmap.ic_light_off);
        } else {
            iv_light.setImageResource(R.mipmap.ic_light_on);
        }
    }

    // torch 手电筒
    @Override
    public void onTorchOn() {
        Toast.makeText(this, "torch on", Toast.LENGTH_LONG).show();
        isLightOn = true;
    }

    @Override
    public void onTorchOff() {
        Toast.makeText(this, "torch off", Toast.LENGTH_LONG).show();
        isLightOn = false;
    }

    // 判断是否有闪光灯功能
    private boolean hasFlash() {
        return getApplicationContext().getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }

    @Override
    protected void onPause() {
        super.onPause();
        captureManager.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        captureManager.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        captureManager.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        captureManager.onSaveInstanceState(outState);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return mDBV.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }
}
